import QtQuick 2.5
import QtQuick.Controls 1.5
import QtQuick.Window 2.0
import "./UI/Home"
import UdpSocket 1.0

ApplicationWindow {
    visible: true
    width: Screen.desktopAvailableWidth
    height: Screen.desktopAvailableHeight
    color:"#000000"

    //home stack
    StackView{
        id:stack
        focus: true
        anchors.fill: parent
        initialItem:homeView
        delegate: StackViewDelegate {
            function transitionFinished(properties)
            {
                properties.exitItem.x = 0
//                properties.exitItem.rotation = 0
            }

            pushTransition: StackViewTransition {
                SequentialAnimation {


                    PropertyAnimation {
                        target: enterItem
                        property: "x"
                        from: enterItem.width
                        to: 0
                        easing.type: Easing.OutQuint
                        duration: 1000
                    }
                }
                PropertyAnimation {
                    target: exitItem
                    property: "x"
                    from: 0
                    to: -exitItem.width
                    easing.type: Easing.OutQuint
                    duration: 1000
                }
                PropertyAnimation{
                    target: enterItem
                    property: "opacity"
                    from:0.5
                    to:1
                    duration: 1000
                }
                PropertyAnimation{
                    target: exitItem
                    property: "opacity"
                    from:1
                    to:0.5
                    duration: 1000
                }
            }
            popTransition: StackViewTransition {
                SequentialAnimation {
                    PropertyAnimation {
                        target: enterItem
                        property: "x"
                        from: -enterItem.width
                        to: 0
                        easing.type: Easing.OutQuint
                        duration: 1000
                    }
                }
                PropertyAnimation {
                    target: exitItem
                    property: "x"
                    from: 0
                    to: exitItem.width
                    easing.type: Easing.OutQuint
                    duration: 1000
                }

                PropertyAnimation{
                    target: enterItem
                    property: "opacity"
                    from:0.5
                    to:1
                    duration: 1000
                }
                PropertyAnimation{
                    target: exitItem
                    property: "opacity"
                    from:1
                    to:0.5
                    duration: 1000
                }
            }

        }
        Keys.onBackPressed: {
            if(stack.depth>1){
                stack.pop();
            }
            else
                Qt.quit();
        }
    }

    //init item
    HomePage{
        id:homeView
    }

    //udpSocket
    Timer{
        interval: 1000
        repeat: true
        id:udptimer
        onTriggered: {

            udpsocket.sendDatas("192.168.0.255",988,"123456AT+LKSTT");
            udpsocket.sendDatas("192.168.1.255",988,"123456AT+LKSTT");
        }
    }

    MUdpSocket{
        id:udpsocket
        Component.onCompleted: udptimer.start();
        onMdataChanged: {
            if(mdata!=="aa"){
                var datas=mdata.split("\"");
                var ipstr=datas[1].toString();
                TcpSocket.conIP="server ip:"+datas[1].toString();
                TcpSocket.connect_To_Host(ipstr,8000);
                udptimer.stop();
            }
        }
    }

    Timer{
        id:reconnectTimer
        running: true
        repeat: true
        interval: 500
        onTriggered: {
            if(TcpSocket.tcpState==="connected")
                return;
            var datas=udpsocket.mdata.split("\"");
            if(!datas[1])
                return;
            var ipstr=datas[1].toString();
            TcpSocket.conIP="server ip:"+datas[1].toString();
            TcpSocket.connect_To_Host(ipstr,8000);
        }
    }
}
